package com.demo.controller;

import java.util.List;

import com.demo.common.model.ExchangeItem;
import com.demo.common.model.ExchangeTrade;
import com.demo.common.model.JsonResult;
import com.demo.common.model.OrderItem;
import com.demo.common.model.SecondTrade;
import com.demo.common.model.User;
import com.demo.constants.Constants;
import com.demo.service.TradeService;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

public class TradeController extends Controller {
	static TradeService service = new TradeService();

	public void sold() {
		User suser = getSessionAttr(Constants.SESSION_LOGIN_USER);
		SecondTrade trade = getBean(SecondTrade.class, "", true);
		service.sold(suser, trade);
		JsonResult jr = new JsonResult(Constants.error_code_success);
		renderJson(jr);
	}

	public void solddetail() {
		String sid = getPara();
		SecondTrade trade = service.getSoldDetail(sid);
		setAttr("trade", trade);
		render("solddetail.html");
	}

	public void exchdetail() {
		String sid = getPara();
		ExchangeTrade trade = service.getExchDetail(sid);
		setAttr("trade", trade);
		render("exchdetail.html");
	}

	public void getsolddetail() {
		User suser = getSessionAttr(Constants.SESSION_LOGIN_USER);
		String sid = getPara();
		SecondTrade trade = service.getSoldDetail(sid);
		JsonResult jr = new JsonResult();
		jr.setErrorCode(Constants.error_code_success);
		jr.setData("trade", trade);
		jr.setData("book", trade.getBook());
		jr.setData("user", suser);
		renderJson(jr);
	}

	public void getexchdetail() {
		User suser = getSessionAttr(Constants.SESSION_LOGIN_USER);
		String sid = getPara();
		List<OrderItem> items = service.getExchOrderItem(sid, suser);
		JsonResult jr = new JsonResult();
		jr.setErrorCode(Constants.error_code_success);
		for (OrderItem orderItem : items) {
			jr.setData("book_" + orderItem.getId(), orderItem.getBook());
		}
		jr.setData("items", items);
		jr.setData("user", suser);
		renderJson(jr);
	}

	public void soldlist() {
		User suser = getSessionAttr(Constants.SESSION_LOGIN_USER);
		Page<SecondTrade> page = service.getSoldList(1, 10, suser,
				Constants.trade_type_sold);
		Page<SecondTrade> page2 = service.getSoldList(1, 10, suser,
				Constants.trade_type_buy);
		setAttr("page", page);
		setAttr("page2", page2);
		render("soldlist.html");
	}

	public void buysold() {
		SecondTrade trade = getBean(SecondTrade.class, "", true);
		User suser = getSessionAttr(Constants.SESSION_LOGIN_USER);
		String errorCode = service.buySold(trade, suser);
		JsonResult jr = new JsonResult(errorCode);
		renderJson(jr);
	}

	public void buyexch() {
		ExchangeItem eItem = getBean(ExchangeItem.class, "", true);
		String tradeId = getPara("tradeId");
		User suser = getSessionAttr(Constants.SESSION_LOGIN_USER);
		String errorCode = service.buyexch(tradeId, eItem, suser);
		JsonResult jr = new JsonResult(errorCode);
		renderJson(jr);
	}

	public void exchange() {
		User suser = getSessionAttr(Constants.SESSION_LOGIN_USER);
		ExchangeItem trade = getBean(ExchangeItem.class, "", true);
		service.exchange(suser, trade);
		JsonResult jr = new JsonResult(Constants.error_code_success);
		renderJson(jr);
	}

	public void exchangelist() {
		User suser = getSessionAttr(Constants.SESSION_LOGIN_USER);
		Page<ExchangeItem> page = service.getExchangeList(1, 10, suser,
				Constants.trade_type_sold);
		Page<ExchangeItem> page2 = service.getExchangeList(1, 10, suser,
				Constants.trade_type_buy);
		setAttr("page", page);
		setAttr("page2", page2);
		render("exchangelist.html");
	}

	public void secondaudit() {
		User suser = getSessionAttr(Constants.SESSION_LOGIN_USER);
		Page<SecondTrade> page = service.getSoldList(1, 10, suser,
				Constants.trade_type_auditing);
		setAttr("page", page);
		render("secondaudit.html");
	}

	public void dosoldaudit() {
		String id = getPara(0);
		Integer status = getParaToInt(1);

		User suser = getSessionAttr(Constants.SESSION_LOGIN_USER);
		service.dosoldaudit(id, status, suser);
		renderJson();
	}

	public void exchangeaudit() {
		Page<ExchangeItem> page = service.getExchangeList(1, 10, null,
				Constants.trade_type_auditing);
		setAttr("page", page);
		render("exchangeaudit.html");
	}

	public void doexchaudit() {
		String id = getPara(0);
		Integer status = getParaToInt(1);

		User suser = getSessionAttr(Constants.SESSION_LOGIN_USER);
		service.doexchaudit(id, status, suser);
		renderJson();
	}

	public void delivery() {
		String orderId = getPara();
		User suser = getSessionAttr(Constants.SESSION_LOGIN_USER);
		String errorCode = service.delivery(orderId, suser);
		JsonResult jr = new JsonResult(errorCode);
		renderJson(jr);
	}

	public void received() {
		String orderId = getPara();
		User suser = getSessionAttr(Constants.SESSION_LOGIN_USER);
		String errorCode = service.received(orderId, suser);
		JsonResult jr = new JsonResult(errorCode);
		renderJson(jr);
	}

	public void close() {
		String orderId = getPara();
		User suser = getSessionAttr(Constants.SESSION_LOGIN_USER);
		String errorCode = service.close(orderId, suser);
		JsonResult jr = new JsonResult(errorCode);
		renderJson(jr);
	}
}
